﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleEntityHeroBase : BattleEntity
{
    public GameObject m_pGameObj;
    public GameObject m_pGameSkele;
    public SkeletonAnimation m_pSkele;
    public Renderer m_pRender;
    public MoveComp m_pMoveComp;
    public HeroVo m_pHeroVo;
    public bool m_bLeft;
    //public NStateMachine<BattleEntityHeroBase> m_pStateMachine;

    public override void OnCreate(params object[] args)
    {
        m_pHeroVo = args[0] as HeroVo;
        GameObject model = args[1] as GameObject;

        //创建U3D对象 模型等
        m_pGameObj = new GameObject();


        m_pGameSkele = GameObject.Instantiate<GameObject>(model);
        m_pGameSkele.transform.SetParent(m_pGameObj.transform);
        m_pGameSkele.transform.localPosition = Vector3.zero;
        m_pGameSkele.transform.localEulerAngles = Vector3.zero;
        m_pGameSkele.transform.localScale = Vector3.one;
        m_pMoveComp = new MoveComp(m_pGameObj.transform, this);
        //m_pMoveComp.m_fMaxSpeed = m_pHeroVo.m_pConfig.maxSpeed;

        //Spine 相关初始化
        m_pSkele = m_pGameSkele.GetComponent<SkeletonAnimation>();
        m_pSkele.ignoreTimeScale = true;
        m_pSkele.skeleton.FlipX = m_pHeroVo.m_bFilp;
        m_pSkele.state.Event += HandleSpine_Event;
        m_pSkele.state.Start += HandleSpine_Start;
        m_pSkele.state.End += HandleSpine_End;
        m_pSkele.state.Complete += HandleSpine_Comp;
        m_pRender = m_pGameSkele.GetComponent<Renderer>();
        m_bLeft = m_pHeroVo.index <= SlgFightDef.c_iHeroIndexStep;
    }

    public override bool HandleMessage(MessageBody body)
    {
        return false;
        //return m_pStateMachine.OnHandleMessage(body);
    }

    public override void OnUpdate(float deltaTime)
    {
        base.OnUpdate(deltaTime);
        //m_pStateMachine.OnUpdate();
        m_pMoveComp.OnUpdate(deltaTime);
    }

    public override void OnDestroy()
    {
        //清楚身上的特效
        BattleEffectMgr.Instance.DelEntityAllEffObj(Id);
    }

    //播放动画
    public void PlayAction(string actionId, bool bLoop = false)
    {
        if (actionId == "0")
            return;

        m_pSkele.loop = bLoop;
        m_pSkele.AnimationName = actionId;
    }

    public virtual void ChangeState(string state, params object[] args)
    {
        //m_pStateMachine.ChangeState(state, args);
    }

    //Spine事件
    private void HandleSpine_Event(Spine.AnimationState state, int trackIndex, Spine.Event e)
    {
        //Debug.Log("Event");
    }
    private void HandleSpine_Start(Spine.AnimationState state, int trackIndex)
    {
        //Debug.Log("Start");
    }
    private void HandleSpine_End(Spine.AnimationState state, int trackIndex)
    {
        //Debug.Log("End");
    }
    private void HandleSpine_Comp(Spine.AnimationState state, int trackIndex, int loopCount)
    {
        //Debug.Log("Comp_ " + state.GetCurrent(trackIndex).Animation.Name);

        EntityMessageDispatcher.Instance.SendMessage(state.GetCurrent(trackIndex).Animation.Name, Id, Id);
    }

    //重置面朝方向
    public void InitRenderDir()
    {
        m_pSkele.skeleton.FlipX = m_pHeroVo.m_bFilp;
    }
}
